package com.xujiang.common_plus.util;

import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.GenericTransitionOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.TransitionOptions;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.xujiang.common_plus.R;

/**
 * 加载图片工具
 *
 * @author xujiang
 * @version 2017-08-08 14:01
 */
public class ImageUtils {

    private static ImageUtils imageUtils;

    private static RequestManager requestManager;

    private RequestBuilder requestBuilder = null;

    public RequestBuilder getRequestBuilder() {
        return requestBuilder;
    }

    public ImageUtils setRequestBuilder(RequestBuilder requestBuilder) {
        this.requestBuilder = requestBuilder;
        return this;
    }

    private static Context mContext;

    /**
     * requestOptions 以及方法总结
     */
    private RequestOptions requestOptions;

    private ImageUtils() {
        requestOptions = new RequestOptions();

    }

    public static ImageUtils get(Context context, RequestManager manager) {
        mContext = context;
        requestManager = manager;
        if (imageUtils == null) {
            imageUtils = new ImageUtils();
        }
        return imageUtils;
    }

    public ImageUtils settransform() {
        requestOptions.transform(new CircleCrop());     //目前 第三方库变化还不支持glide 的第4个版本，官方目前就看到一个
        // .transform(new MultiTransformation<Bitmap>(new FitCenter(),new CircleCrop()))  //多Transformation
        return this;
    }

    public ImageUtils setOverride() {
        requestOptions.override(100, 100);
        return this;
    }

    public ImageUtils setPlaceholder() {  //设置默认填充图片
        requestOptions.placeholder(R.drawable.default_user_avatar);
        return this;
    }

    public ImageUtils setError() {  // 错误填充图片
        requestOptions.error(R.drawable.error);
        return this;
    }

    public ImageUtils setPriority() {  //// 优先级
        requestOptions.priority(Priority.NORMAL);
        return this;
    }

    public ImageUtils setFallback() {  //url为空图片
        requestOptions.fallback(R.drawable.default_user_avatar);
        return this;
    }

    public ImageUtils setDiskCacheStrategy() {
        requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE); //缓存策略，后面详细介绍(R.drawable.default_user_avatar);
        return this;
    }

    /**
     * CenterCrop()会缩放图片让图片充满整个ImageView的边框，然后裁掉超出的部分。ImageVIew会被完全填充满，但是图片可能不能完全显示出
     * fitCenter()会缩放图片让两边都相等或小于ImageView的所需求的边框。图片会被完整显示，可能不能完全填充整个ImageView。
     */
    public ImageUtils setCrop() {  // 填充方式
        requestOptions.centerCrop();
        //requestOptions.fitCenter();
        return this;
    }

    /**
     * GenericTransitionOptions
     * DrawableTransitionOptions
     * BitmapTransitionOptions
     *
     * 如果想去掉默认的变换效果：TransitionOptions.dontTransition().
     * Transitions被要求用在 RequestBuilder
     * such as new DrawableTransitionOptions().dontTransition()  取消变换效果
     *
     * 分清楚两个词 transform 与 transition， 前者是 变形的  后者是 过度、转变
     */

    /**
     * transitionOptions 方法使用与总结
     * TransitionOptions是用来设置占位图片或者缩略图片切换到真正需要加载的图片之间的过度效果。
     * 通过RequestBuilder的transition加载
     * 1. 常用效果
     *
     * crossFade()
     * animate()  // 不知道在哪里
     * 2. 移除效果
     *
     * dontTransition()
     * 3. 三个子类
     *
     * GenericTransitionOptions
     * DrawableTransitionOptions
     * BitmapTransitionOptions
     */
    private TransitionOptions transitionOptions;

    public ImageUtils setDrawableTransitionOptions() {
        transitionOptions = new DrawableTransitionOptions().crossFade(2000);  ////淡入淡出2秒
        //transitionOptions = DrawableTransitionOptions.withCrossFade();
        //transitionOptions = new DrawableTransitionOptions().dontTransition();  //移除效果
        return this;
    }

    public ImageUtils setBitmapTransitionOptions() {
        transitionOptions = new BitmapTransitionOptions().crossFade(2000);  ////淡入淡出2秒
        //transitionOptions = new BitmapTransitionOptions().dontTransition();  //移除效果
        return this;
    }

    public ImageUtils setGenericTransitionOptions() {
        transitionOptions = new GenericTransitionOptions();
        //transitionOptions = new GenericTransitionOptions().dontTransition();  //移除效果
        return this;
    }

    /**
     * requestBuilder 方法
     */

    public ImageUtils setThumbnail(String thumb_url) {
        requestBuilder.thumbnail(Glide.with(mContext).load(thumb_url));

        return this;
    }

    public ImageUtils setApply() {
        requestBuilder.apply(requestOptions);
        return this;
    }

    public ImageUtils setTransitionoption() {
        requestBuilder.transition(transitionOptions);
        return this;
    }

    public ImageUtils setListener() {
        requestBuilder.listener(requestListener);
        return this;
    }

    public RequestListener requestListener;

    public ImageUtils setRequestListener() {
        requestListener = new RequestListener() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target,
                    boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource,
                    boolean isFirstResource) {
                return false;
            }
        };
        return this;
    }

    public ImageUtils setinto(ImageView view) {
        requestBuilder.into(view);
        return this;
    }

    public ImageUtils setLoad(String url) {
        requestBuilder.load(url);
        return this;
    }

    public enum What {
        Bitmap,
        File,
        DownLoadOnly,
        Download,
        Gif,
    }

    /**
     * 默认情况下glide返回的是一个Drawable
     *
     * @param what 输入上面的枚举类型  确定Glide加载的是什么
     */
    public ImageUtils asWhat(What what) {
        switch (what) {
            case Bitmap:
                // RequestBuilder<Bitmap>
                requestBuilder = requestManager.asBitmap();

                break;
            case File:
                //RequestBuilder<File>
                requestBuilder = requestManager.asFile();
                break;
            case Gif:
                requestBuilder = requestManager.asGif();
                break;
            case DownLoadOnly:
                //RequestBuilder<File>
                requestBuilder = requestManager.downloadOnly();
                break;
            //case Download:
            //    //RequestBuilder<File>
            //    requestBuilder = requestManager.download("");
            //    break;
            default:
                //RequestBuilder<Drawable>
                requestBuilder = requestManager.asDrawable();
                break;
        }
        return this;
    }

}
